import re

def extract_and_format_numbers(input_file_path, output_file_path):
    # 读取文本文件内容
    with open(input_file_path, 'r', encoding='utf-8') as file:
        content = file.read()

    # 使用正则表达式提取题号
    pattern = re.compile(r'\b(\d+\.?)\b')
    matches = pattern.findall(content)

    # 过滤出符合递增顺序的题号
    filtered_matches = []
    last_num = 0
    for match in matches:
        try:
            num = int(match.strip('.'))
            if num == last_num + 1:
                filtered_matches.append(match)
                last_num = num
        except ValueError:
            # 忽略无法转换为整数的项
            continue

    # 在每个过滤后的题号后面添加点号（.）
    filtered_numbers_with_dots = [number + '.' for number in filtered_matches]

    # 将带点号的过滤后的题号写入到指定的输出文件中
    with open(output_file_path, 'w', encoding='utf-8') as output_file:
        for number in filtered_numbers_with_dots:
            output_file.write(number + '\n')

# 示例用法：
input_file_path = 'C:/Users/yra/Desktop/MathVecCluster/data/txt/2019年北京市中考数学试卷.txt'  # 请将文件路径替换为实际文件路径
output_file_path = 'C:/Users/yra/Desktop/MathVecCluster/data/out_txt/2019年北京市中考数学试卷.txt'  # 请将输出文件路径替换为实际输出文件路径
extract_and_format_numbers(input_file_path, output_file_path)
